/**
 * MyExpListAdapter.java
 * @author Marco Secchiero
 * 
 * This class implements the menu on the ExpandableListView.
 * It manages all actions available on the menu and its item
 */

package wfta.client.gui;

import java.util.ArrayList;
import java.util.Vector;

import wfta.common.Item;
import wfta.common.Section;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;

public class MyExpListAdapter implements ExpandableListAdapter{
	
	private MenuViewActivity _menuView;
	private Vector<Section> _menu;
	private Vector<Counters> _counters = new Vector<Counters>();
	
	/**
	 * Data structure of a row of counters
	 * @author Marco Secchiero
	 */
	private class Counters {
		private Vector<Integer> _counterCategory = new Vector<Integer>();
		
		/**
		 * Contructor
		 * @param length: the length of the Vector
		 */
		public Counters(int length) {
			_counterCategory.setSize(length);
			for (int i = 0; i < length; i++){
				setValue(i,0);
			}
		}
		
		/**
		 * @param pos
		 * @return the value of the element in position pos
		 */
		public int getValue(int pos) {
			return _counterCategory.get(pos);
		}
		
		/**
		 * Set the element in position pos to the value
		 * @param pos
		 * @param value
		 */
		public void setValue(int pos, int value) {
			_counterCategory.set(pos, value);
		}
	}
	
	/**
	 * Array List to manage the changes of the list
	 */
	private ArrayList<DataSetObserver> _observers = new ArrayList<DataSetObserver>();
	
	/**
	 * Notifies the view that something has changed, the view will refresh data
	 */
	private void notifyObservers() {
		for (DataSetObserver observer : _observers) {
			observer.onChanged();
		}
	}
	
	/**
	 * Sets layout of a text view
	 * @return the text view
	 */
	private TextView getGenericView() {
		// setting layout
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, // width
				64 									// height
				); 
		TextView textView = new TextView(_menuView);
		textView.setLayoutParams(lp);
		textView.setPadding(36, 0, 0, 0);
		return textView;
	}
	
	/**
	 * Initializes counters to 0 
	 */
	private void initializeCounters() {
		_counters.setSize(_menu.size());
		for (int i = 0; i < _menu.size(); i++){
			_counters.add(i,new Counters(_menu.get(i).getItems().size()));
		}
	}
	
	/**
	 * Constructor
	 * @param menuViewActivity: parent View
	 * @param section: the menu
	 */
	public MyExpListAdapter(MenuViewActivity menuViewActivity,Vector<Section> section) {
		// TODO Auto-generated constructor stub
		_menuView = menuViewActivity;
		_menu = section;
		Log.w("EXPLISTADP","Getted menu");
		initializeCounters();
	}
	
	public int getCounter(int groupPosition, int childPosition) {
		return _counters.get(groupPosition).getValue(childPosition);
	}
	
	public void incrementCounter(int groupPosition, int childPosition) {
		int val = _counters.get(groupPosition).getValue(childPosition); 
		_counters.get(groupPosition).setValue(childPosition,val+1);
		notifyObservers();
	}
	
	public void decrementCounter(int groupPosition, int childPosition) {
		int val = _counters.get(groupPosition).getValue(childPosition);
		if (val>0) {
			_counters.get(groupPosition).setValue(childPosition, val-1);	
		}
		notifyObservers();
	}

	@Override
	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return _menu.get(groupPosition).getItems().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return _menu.get(groupPosition).getItems().get(childPosition).getID();
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean arg2, View arg3,
			ViewGroup arg4) {
		// TODO Auto-generated method stub
		TextView textView = getGenericView();
		textView.setText(((Item)getChild(groupPosition,childPosition)).getName());
		textView.append ("\t\t"+
			((Double)((Item)getChild(groupPosition,childPosition)).getPrice()).toString()+"€"+
			"\t\t"+"qty: "+getCounter(groupPosition,childPosition));
		return textView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return _menu.get(groupPosition).getItems().size();
	}

	@Override
	public long getCombinedChildId(long groupId, long childId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long getCombinedGroupId(long groupId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return _menu.get(groupPosition).getName();
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return _menu.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		TextView textView = getGenericView();
		textView.setText(getGroup(groupPosition).toString());
		textView.setTextColor(Color.rgb(0xFF, 0, 0));
		return textView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onGroupCollapsed(int groupPosition) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		// TODO Auto-generated method stub

	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub
		_observers.add(observer);
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub
		_observers.remove(observer);
	}
	 
}
